package xyz.csmcool.imchat.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.csmcool.imchat.constants.Filetype;
import xyz.csmcool.imchat.entity.FileMessageVo;
import xyz.csmcool.imchat.entity.vo.RoomChatsVo;
import xyz.csmcool.imchat.service.IRoomsChatsService;
import xyz.csmcool.imutil.dto.LastMessageDto;
import xyz.csmcool.imutil.dto.Message;
import xyz.csmcool.imutil.utils.JwtUtils;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.MyPage;
import xyz.csmcool.imutil.webVo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csmcool
 * @since 2022-08-14
 */
@RestController
@RequestMapping("/roomsChats")
@RequiredArgsConstructor
public class RoomsChatsController {

    final IRoomsChatsService roomsChatsService;

    @PostMapping("/sendMessage")
    public ResultVo sendMessage(@RequestBody Message<String> message, HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        Message<String> stringMessage = roomsChatsService.sendMessage(message, uid);
        return ResultTool.success(stringMessage);
    }

    @PostMapping("/sendImageMessage")
    public ResultVo sendMessage(HttpServletRequest request,@RequestParam("fileImage") MultipartFile[] files,
                                @RequestParam("roomId") Integer roomId) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        roomsChatsService.sendImageMessage(files,uid,roomId);
        return ResultTool.success();
    }


    @PostMapping("/sendSoundImage")
    public ResultVo sendSoundMessage(HttpServletRequest request,@RequestParam("recodeSound") MultipartFile file,
                                @RequestParam("roomId") Integer roomId) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        roomsChatsService.sendSoundMessage(file,uid,roomId);
        return ResultTool.success();
    }

    @PostMapping("/sendRoomFileMessage")
    public ResultVo sendRoomFileMessage(HttpServletRequest request,@RequestBody FileMessageVo messageVo) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        roomsChatsService.sendRoomFileMessage(messageVo,id);
        return ResultTool.success();
    }

    @GetMapping("/getLastMessage")
    public ResultVo<List<LastMessageDto>> getLastMessage(@RequestParam("roomIds") List<Integer> ids) {
        List<LastMessageDto> lastMessages = roomsChatsService.getLastMessage(ids);
        return ResultTool.success(lastMessages);
    }

    @GetMapping("/getFirstHistoryMessage/{id}")
    public ResultVo<List<RoomChatsVo>> getFirstHistoryMessage(@PathVariable("id") Integer id,HttpServletRequest request) {
        String token = JwtUtils.getMemberIdByJwtToken(request);
        List<RoomChatsVo> firstHistory = roomsChatsService.getFirstHistory(id,Integer.valueOf(token));
        return ResultTool.success(firstHistory);
    }

    @GetMapping("/getHistoryMessage/{limit}/{page}/{roomId}/{id}")
    public ResultVo<MyPage<RoomChatsVo>> getHistoryMessage(@PathVariable("limit") Long limit,
                                                         @PathVariable("page") Long page,
                                                         @PathVariable("roomId") Integer roomId,
                                                         @PathVariable("id") Integer id,
                                                           HttpServletRequest request) {
        String token = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<RoomChatsVo> historyMessage = roomsChatsService.getHistoryMessage(page, limit, roomId, id,
                Integer.valueOf(token));
        return ResultTool.success(historyMessage);
    }

    @GetMapping("/getHistoryImg/{limit}/{page}/{roomId}")
    public ResultVo<MyPage<String>> getHistoryImg(@PathVariable("limit") Long limit,
                                          @PathVariable("page") Long page,
                                          @PathVariable("roomId") Integer roomId,HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<String> historyImg = roomsChatsService.getHistoryImg(page, limit, roomId, Integer.valueOf(id));
        return ResultTool.success(historyImg);
    }

    @GetMapping("/getHistoryFile/{limit}/{page}/{roomId}")
    public ResultVo<MyPage<String>> getHistoryFile(@PathVariable("limit") Long limit,
                                                  @PathVariable("page") Long page,
                                                  @PathVariable("roomId") Integer roomId,HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<String> historyImg = roomsChatsService.getHistoryFile(page, limit, roomId, Integer.valueOf(id));
        return ResultTool.success(historyImg);
    }

    @PostMapping("/delete-room-chats")
    public ResultVo deleteRoomChats(@RequestParam("id") Integer roomId) {
        Boolean aBoolean = roomsChatsService.deleteRoomChats(roomId);
        return ResultTool.success();
    }

    @PostMapping("/clear-room-chats")
    public ResultVo clearRoomChats(@RequestParam("id") Integer id,HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        roomsChatsService.clearRoomChats(id, Integer.valueOf(uid));
        return ResultTool.success();
    }
}
